//Servlet handles the addition of a friend to the particular nym once the friend has approved the request.
//Author:Shreyas Kamath

import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

public class AddFriendServlet extends HttpServlet{

	//Vector that adds the name of the buddyname
	public static Vector vector_Buddyname =new Vector(20,5);
	public static int iNumBuddies=0;	
	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("AddFriendServlet: init()");
	}
	
	//This method gets the parameter from the add_friend.html page and then calls the addfriends function to actually add the friend to the nym.
	public void doPost(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		int ibuddyno;
		String strPrevNym=null;
		// set the response type
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");
		//get tha parameters for nym and the friends
		String strNym=request.getParameter("nym");
		//String strNym="Home";
		String strFriends=request.getParameter("friends");
		//String strFriends="Alex_work,Alex";
		int success=this.AddFriends(strNym,strFriends);
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		//Send back the xml.The success parameter indicates whether the friend was succesfully added to the list or no.
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Success>");
		out.println("<valid>");
		out.println(success);
		out.println("</valid>");
		out.println("</Success>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;
		
		try
		{
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			// Do nothing
			System.out.println("Connection failed");
		};  
		//return the connection
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// AddFriends()
	// Adds the friends to the nym
	// The nymname and the friends are the input parameters
	//The function adds the friends to the particular nym.
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static int AddFriends(String strNymName,String strFriends)
	{
		ResultSet rsFriends;
		//get the connection parameter
		Connection conn=Connect();
		String strBuddyID=null;
		//get the Nym ID
		String strNymID=GetNymID(strNymName);
		StringTokenizer strTokBuddyName=null;
		strTokBuddyName=new StringTokenizer(strFriends,",");
		try
		{
			Statement stAddBuddyName=conn.createStatement();
			Statement stBuddyID=conn.createStatement();
			//While there are more friends
			while(strTokBuddyName.hasMoreTokens())
			{
				//add friend to the nym
				rsFriends=stBuddyID.executeQuery("select NymID from nymnamemap where nymnamemap.NymName= '"+strTokBuddyName.nextToken()+"'"); 
				while(rsFriends.next())
				{
					strBuddyID=rsFriends.getString("NymID");
				}
				//Get the names of the broadcasters.
				stAddBuddyName.executeUpdate("insert into nyminfo values('"+strNymID+"','"+strBuddyID+"')"); 
			}
			return 1;
		}
		catch(Exception e)
		{
			System.out.println(" The following Exception occured"+e);
			return 0;
		}
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function AddFriends()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */		
	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetNymID()
	// Gets the 128 bit NymID corresponding to a nym using the nym name as the parameter
	// returns 128 bit NymID
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static String GetNymID(String strNym)
	{
		Connection conn =Connect();
		String strNymid=null;
		ResultSet rsNymID;
		try
		{
			Statement getNymID= conn.createStatement();
		      //Get the nymid corresponding to the nym name from the database 
			rsNymID=getNymID.executeQuery("select nymid from nymnamemap where nymname='" + strNym + "'");
			while( rsNymID.next() )
			{
				strNymid=rsNymID.getString("nymid");
			}
		}
		catch( Exception e )
		{
			System.out.println ( "Database Exception is " + e );
		};  
		return(strNymid);
	}	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function GetNymID()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */		
		
	public void destroy() {
		System.out.println("BroadCasterServlet: destroy()");
	}
}